package com.zhj.dao;

import java.util.Iterator;
import java.util.List;

import com.zhj.bean.Student;
import com.zhj.datasource.MySqlConfig;
public class StudentDao_SQL extends MySqlConfig implements StudentDao {
	
	/**
	 * 添加学生
	 */
	public void addStudent(Student stu) throws Exception {
		try{
			sqlMapClient.insert("addStudent", stu);
		}catch (Exception e) {
			throw new Exception("保存学生失败");
		}
	}

	public void addStudentBySequence(Student stu) throws Exception {

	}
	/**
	 * 根据ID删除学生
	 */
	public void deleteStudentById(String id) throws Exception {
		sqlMapClient.delete("deleteStudentById", id);
	}
	/**
	 * 查询所有学生
	 */
	public List<Student> queryAllStudent(Student stu) throws Exception {
		List<Student> stuList = null;
		stuList = sqlMapClient.queryForList("queryAllStudent");
		return stuList;
	}
	/**
	 * 根据ID查询学生
	 */
	public Student queryStudentById(String id) throws Exception {
		Student stu=new Student();
		stu=(Student) sqlMapClient.queryForObject("queryStudentById", id);
		return stu;
	}
	/**
	 * 根据ID修改学生
	 */
	public void updateStudentById(String id) throws Exception {
		sqlMapClient.update("updateStudentById", id);
	}

	public static void main(String[] args) {
	
		StudentDao studentDao = new StudentDao_SQL();
		Student stu = new Student();
		stu.setStu_id("4");
		try {
			List<Student> list = studentDao.queryAllStudent(stu);
			 Iterator<Student> i=list.iterator();
			 while(i.hasNext()){
				 Student stus=i.next();
				 System.out.println("id--->"+stus.getStu_id()+"\n"+"name--->"+stus.getStu_name()+"\nsex--->"+stus.getStu_sex());
			 }
			System.out.println(list.size());
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
}
